﻿namespace WMS6.Web.Inventory
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.InvTransferServiceRef;
    using WMS6.Web.UserControls;

    public class OwnerTransFerList : BasePage
    {
        protected Button btnDelete;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnNewSearch;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected HtmlForm F5067;
        protected GridView gvOwnerTransfer;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected Label lblMsg;
        protected Label lblTransfer;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindDataTransferList(OwnerTransferQueryEntity transferQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(transferQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = InvTransferServiceClientProxy.GetInstance().GetInvOwnerTransferInfoByQueryEntity(transferQuery);
            base.BindData2GridView(this.gvOwnerTransfer, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvOwnerTransfer, "TRANSFER_ID");
            List<string> strStatus = base.GetSelectedIDsInGridView(this.gvOwnerTransfer, "STATUS");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C008");
            }
            else
            {
                for (int i = 0; strStatus.Count > i; i++)
                {
                    if (strStatus[i].ToUpper() != "NEW")
                    {
                        this.lblMsg.Text = "只能删除状态为New的单据";
                        return;
                    }
                }
                try
                {
                    string temp = InvTransferServiceClientProxy.GetInstance().DeteleOwnerTransferInfo(selectedID);
                    this.LoadData();
                }
                catch (Exception)
                {
                    this.lblMsg.Text = "删除成功！";
                }
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            OwnerTransferQueryEntity transferQuery = this.GetOwnerTransferQueryEntity();
            transferQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = transferQuery.CurrentPage;
            transferQuery.CurrentPage = "0";
            this.gvOwnerTransfer.AllowSorting = false;
            this.BindDataTransferList(transferQuery);
            this.hdnActionID.Value = "";
            this.gvOwnerTransfer.ShowFooter = false;
            transferQuery.IsGetAll = false;
            transferQuery.CurrentPage = currentPageIndex;
            this.SetOwnerTransferQueryEntity(transferQuery);
            base.Export2Excel("OwnerTransFer.xls", this.gvOwnerTransfer);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            OwnerTransferQueryEntity transferQuery = this.GetOwnerTransferQueryEntity();
            transferQuery.CurrentPage = "0";
            this.BindDataTransferList(transferQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            OwnerTransferQueryEntity transferQuery = this.GetOwnerTransferQueryEntity();
            transferQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindDataTransferList(transferQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            OwnerTransferQueryEntity transferQuery = this.GetOwnerTransferQueryEntity();
            if (int.Parse(transferQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                transferQuery.CurrentPage = (int.Parse(transferQuery.CurrentPage) + 1).ToString();
                this.BindDataTransferList(transferQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            OwnerTransferQueryEntity transferQuery = this.GetOwnerTransferQueryEntity();
            if (int.Parse(transferQuery.CurrentPage) > 0)
            {
                transferQuery.CurrentPage = (int.Parse(transferQuery.CurrentPage) - 1).ToString();
                this.BindDataTransferList(transferQuery);
            }
        }

        private OwnerTransferQueryEntity GetOwnerTransferQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as OwnerTransferQueryEntity);
            }
            return (base.PageQueryEntitySession as OwnerTransferQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                OwnerTransferQueryEntity transferQuery = this.GetOwnerTransferQueryEntity();
                transferQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindDataTransferList(transferQuery);
            }
        }

        protected void gvOwnerTransfer_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F5062", this.gvOwnerTransfer);
        }

        protected void gvOwnerTransfer_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvOwnerTransfer_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                OwnerTransferQueryEntity TransferQuery = this.GetOwnerTransferQueryEntity();
                TransferQuery.SortField = e.SortExpression;
                if (TransferQuery.SortDirection == "ASC")
                {
                    TransferQuery.SortDirection = "DESC";
                }
                else if (TransferQuery.SortDirection == "DESC")
                {
                    TransferQuery.SortDirection = "ASC";
                }
                this.BindDataTransferList(TransferQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            OwnerTransferQueryEntity transferQuery = new OwnerTransferQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    transferQuery.TransferID = this.PreviousPage.TransferID;
                    transferQuery.Status = this.PreviousPage.Status;
                    transferQuery.TransferType = this.PreviousPage.TransferType;
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            transferQuery.IsGetAll = false;
            transferQuery.CurrentPage = "0";
            transferQuery.PageSize = this.gvOwnerTransfer.PageSize.ToString();
            transferQuery.SortField = "TRANSFER_ID";
            transferQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvOwnerTransfer.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetOwnerTransferQueryEntity(transferQuery);
            this.BindDataTransferList(transferQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetOwnerTransferQueryEntity(OwnerTransferQueryEntity transferQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = transferQuery;
            }
            else
            {
                base.PageQueryEntitySession = transferQuery;
            }
        }

        public OwnerTransFerQuery PreviousPage
        {
            get
            {
                return (OwnerTransFerQuery) base.PreviousPage;
            }
        }
    }
}

